/**
 * Created by rongyao on 2017/11/14.
 */
export const increase = ()=>{
	return {
		type:"INCREASE"
	}
}

export const getNewsList = (data)=>{
	return {
		type:"GETNEWSLIST",
		data
	}
}

export const getProductList = (data)=>{
	return {
		type:"GETPRODUCTLIST",
		data
	}
}

export const getCateList = (data)=>{
	return {
		type:"GETCATELIST",
		data
	}
}

export const getCommentList = (data)=>{
	return {
		type:"GETCOMMENTLIST",
		data
	}
}

export const addCar = (data)=>{
	return {
		type:"ADDSHOPCAR",
		data
	}
}

export function fetchNews(page,keyWords,cateIds) {
	// console.log(111);
	return dispatch=>{
		// console.log(222);
		var url = `http://localhost:3000/news/list`;
		return fetch(url,{
			method:"POST",
			headers: {
				"Content-Type": "application/json"
			},
			body: JSON.stringify({page:page-0,keyWords:keyWords,ids:cateIds})
		}).then(res=>{
			return res.json()
		}).then(data=>{
			dispatch(getNewsList(data))
			console.log(data);
		})
	}
}


export function fetchProduct(page,keyWords,cateIds) {
	// console.log(111);
	return dispatch=>{
		// console.log(222);
		var url = `http://localhost:3000/product/list`;
		return fetch(url,{
			method:"POST",
			headers: {
				"Content-Type": "application/json"
			},
			body: JSON.stringify({page:page-0,keyWords:keyWords,ids:cateIds,rows:8})
		}).then(res=>{
			return res.json()
		}).then(data=>{
			dispatch(getProductList(data))
			console.log(data);
		})
	}
}

export function fetchCate(type) {
	return dispatch=>{
		var url = `http://localhost:3000/cate/list`;
		return fetch(url,{
			method:"POST",
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: `type=${type}`
		}).then(res=>{
			return res.json()
		}).then(data=>{
			dispatch(getCateList(data))
		})
	}
}

export function fetchIds(id,type) {
	return dispatch=>{
		var url = `http://localhost:3000/cate/list/${id}`;
		return fetch(url,{
			method:"POST"
		}).then(res=>{
			return res.json()
		}).then(data=>{
			console.log(data);
			if(type==1){
				dispatch(fetchNews(null,null,data))
			}else{
				dispatch(fetchProduct(null,null,data))
			}
			
		})
	}
}

export function fetchComment(id) {
	return dispatch=>{
		var url = `http://localhost:3000/comment/list`;
		return fetch(url,{
			method:"POST",
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: `cateId=${id}`
		}).then(res=>{
			return res.json()
		}).then(data=>{
			dispatch(getCommentList(data))
		})
	}
}


export function AddComment(commentData) {
	return dispatch=>{
		var url = `http://localhost:3000/comment/data`;
		return fetch(url,{
			method:"POST",
			headers: {
				"Content-Type": "application/json"
			},
			body: JSON.stringify(commentData)
		}).then(res=>{
			return res.json()
		}).then(data=>{
			dispatch(fetchComment(commentData.cateId))
		})
	}
}